layui.use(['form', 'jquery', 'layer'], function() {
	let form = layui.form;
	let $ = layui.jquery;
	let layer = layui.layer;
	let oldpwd=document.querySelector('.oldPassword');
	let newPassword=document.querySelector('.newPassword');
	form.verify({
		pass: [
			/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
		],
	});
	//获取到token
	var token = localStorage.getItem('token');
	//给修改绑定点击事件
	form.on('submit(changePassword)', function(data) {
		//判断两次密码是否相同
		if(!oldpwd.value==newPassword.value){
			return alert('密码不一致');
		}
		ajax({
			type: 'post',
			url: 'http://api-breakingnews-web.itheima.net/my/updatepwd',
			token: token,
			data:{
				oldPwd:data.field.oldPassword,
				newPwd:data.field.newPassword,
			},
			success: function(res) {  
				if (res.status == 0) {
					layer.msg(res.message);
					//重定向到用户首页
					window.parent.location.replace("../login.html");
				} else {
					layer.msg(res.message)
				}
			}
		})
		return false;
	});
});
